package com.zhyl.demo.controller;

import com.zhyl.demo.entity.News;
import com.zhyl.demo.service.NewsService;
import com.zhyl.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/news")
public class NewsController {
    private final NewsService newsService;
    @Autowired
    private NewsController(NewsService newsService) {

        this.newsService = newsService;
    }

    @RequestMapping("/list")
    public Result list(){
        return Result.success(newsService.list());
    }

    @RequestMapping("/allList")
    public Result allList(){
        return Result.success(newsService.allList());
    }

    @RequestMapping("/addNews")
    public Result addNews(@RequestBody News news){
        if (news==null){
            return Result.error("-1","参数错误");
        }
        // 调用service层的方法
        Boolean b = newsService.addNews(news);
        if (b){
            return Result.success();
        }else {
            return Result.error("-1","发布失败");
        }
    }

    @RequestMapping("/editNews")
    public Result editNews(@RequestBody News news){

        if (news==null){
            return Result.error("-1","参数错误");
        }
        // 调用service层的方法
        Boolean b = newsService.editNews(news);
        if (b){
            return Result.success();
        }else {
            return Result.error("-1","修改失败");
        }
    }

    @RequestMapping("/deleteNews")
    public Result deleteNews(Integer id){

        if (id==null){
            return Result.error("-1","参数错误");
        }
        // 调用service层的方法
        Boolean b = newsService.deleteNews(id);
        if (b){
            return Result.success();
        }else {
            return Result.error("-1","删除失败");
        }
    }
}
